let lastspan = document.querySelector('main .tip p .last')
$(lastspan).html(`${decodeURI(location.href).split('=')[2]}`)




// 设置变量，用来接收当前select中第一个的值
let selectVal
// 设置pageCount
let pageCount
//  设置当前所在第几页,默认第一页
let nowPage = 1
// 设置变量，用来接收pagesize
let pagesize

//  设置页面初始化事件,完成第一页的渲染并获取共有几页
$(function () {
    getTotalCount()
    nowPage = Math.ceil(pageCount / pagesize)
    // 调用渲染页面函数,始终是第一页
    pageChange(1)
    // 根据页数，创建对应数量的option
    for (let i = 1; i <= nowPage; i++) {
        $('main section .btns select').append($(`<option>${i}/${nowPage}</option>`))
    }




    selectVal = $('main section .btns select').val().split('/')[0]


    // 设置上一页事件
    let upPage = $('main section .btns .uppage')
    upPage.click(function () {
            if(selectVal == 1){
                selectVal = 1
                $('main section .btns select').val(`${selectVal}/${nowPage}`)
                $(this).css('opacity','0.5')
                $('main section .btns .nextpage').css('opacity','1')
            }else if(selectVal>1){
                selectVal--
                $('main section .btns select').val(`${selectVal}/${nowPage}`)
                pageChange(selectVal)
                $(this).css('opacity','1')
                $('main section .btns .nextpage').css('opacity','1')
            }
            
        
    })

    let nextPage = $('main section .btns .nextpage')
    // 设置下一页事件
    nextPage.click(function () {
            if(selectVal<nowPage){
                selectVal++
                $('main section .btns select').val(`${selectVal}/${nowPage}`)
                pageChange(selectVal)
                $(this).css('opacity','1')
                $('main section .btns .uppage').css('opacity','1')
            }else if(selectVal == nowPage){
                $(this).css('opacity','0.5')
                $('main section .btns .uppage').css('opacity','1')
            }
    })




    //  设置select事件
    $('main section .btns select').change(function () {
        selectVal = $('main section .btns select option:selected').text().split('/')[0]

        pageChange(selectVal)
    })
})



// 设置获取totalCount函数
function getTotalCount() {
    $.ajax({
        async: false,
        method: 'get',
        url: `http://chst.vip:1234/api/getproductlist?categoryid=${decodeURI(location.href).split('?')[1].split('=')[1]}`,
        success: function (val) {
            pageCount = val.totalCount
            pagesize = val.pagesize

        }
    })
}





// 设置改变页面函数
function pageChange(pagecount) {
    $.ajax({
        async: false,
        method: 'get',
        url: `http://chst.vip:1234/api/getproductlist?categoryid=${decodeURI(location.href).split('?')[1].split('=')[1]}&pageid=${pagecount}`,
        success: function (res) {
            let pageCount = res.totalCount
            let kong = ``
           
            res.result.forEach(item => {
                kong += `
                    <li productId=${item.productId} name=${item.productName}>
                        <div class="left">
                                <img src="${item.productImg.match(/[0-9a-zA-Z:/._]+/g)[2]}" alt="">
                        </div>
                        <div class="right">
                                <p>${item.productName}</p>
                                <h4>${item.productPrice}</h4>
                                <p>
                                    <span>${item.productQuote}</span>
                                    <span>
                                        <i class="iconfont icon-liwu"></i>
                                        ${item.productCom}
                                    </span>
                                </p>
                        </div>
                    </li>
               `
            });
            $('main section ul:eq(0)').html(kong)
            let $lis = $('main section ul:eq(0) li')
            $lis.click(function () {
                location.href = `http://127.0.0.1:5500/domo/千锋商城项目/html/xiangqing.html?productId=${$(this).attr('productId')}&name=${$(this).attr('name')}`
            })
        }
    })
}





let $span = $('main .tip p span')

$span.click(function () {
    if ($(this).html() === '首页&gt;') {
        location.href = `http://127.0.0.1:5500/domo/千锋商城项目/html/主页.html`
    } else if ($(this).html() === '全部分类&gt;') {
        location.href = `http://127.0.0.1:5500/domo/千锋商城项目/html/category.html`
    } else if ($(this).html() === `${decodeURI(location.href).split('=')[2]}`) {
        location.href = `http://127.0.0.1:5500/domo/千锋商城项目/html/particular.html?categoryId=${decodeURI(location.href).split('?')[1].split('=')[1]}&category=${decodeURI(location.href).split('=')[2]}`
    }
})

toTop()



